/* 
   Copyright 2012 Ryne Okimoto

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package tests;

import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
import recipe.Ingredient;
import recipe.RecipeData;

public class TestRecipeData {

	@Test
	public void testRecipeData() {
		RecipeData rec = new RecipeData();
		assertTrue(
				rec.getRecipeName().isEmpty() &&
				rec.getCategory().isEmpty() &&
				rec.getImageFileName().isEmpty() &&
				rec.getIngredients().isEmpty() &&
				rec.getInstructions().isEmpty() &&
				rec.getTags().isEmpty()
				);
	}

	@Test
	public void testGetRecipeName() {
		String nm = "Name";
		RecipeData rec = new RecipeData();
		rec.setRecipeName(nm);
		assertTrue(nm.equals(rec.getRecipeName()));
	}

	@Test
	public void testSetRecipeName() {
		String nm = "Name";
		RecipeData rec = new RecipeData();
		rec.setRecipeName(nm);
		assertTrue(rec.getRecipeName().equals(nm));
	}

	@Test
	public void testGetImageFileName() {
		String img = "Image";
		RecipeData rec = new RecipeData();
		rec.setImageFileName(img);
		assertTrue(img.equals(rec.getImageFileName()));
	}

	@Test
	public void testSetImageFileName() {
		String img = "Image";
		RecipeData rec = new RecipeData();
		rec.setImageFileName(img);
		assertTrue(rec.getImageFileName().equals(img));
	}

	@Test
	public void testGetCategory() {
		String cat = "Category";
		RecipeData rec = new RecipeData();
		rec.setCategory(cat);
		assertTrue(cat.equals(rec.getCategory()));
	}

	@Test
	public void testSetCategory() {
		String cat = "Category";
		RecipeData rec = new RecipeData();
		rec.setCategory(cat);
		assertTrue(rec.getCategory().equals(cat));
	}

	@Test
	public void testGetIngredients() {
		String ing = "Ingredients";
		RecipeData rec = new RecipeData();
		rec.setIngredients(ing);
		assertTrue(ing.equals(rec.getIngredients()));
	}

	@Test
	public void testSetIngredients() {
		String ing = "Ingredients";
		RecipeData rec = new RecipeData();
		rec.setIngredients(ing);
		assertTrue(rec.getIngredients().equals(ing));
	}
	
	@Test
	public void testGetListOfIngredients(){
		RecipeData r = new RecipeData();
		r.addIngredient("cheese", "1", Ingredient.UNIT_STANDARD_POUND);
		assertNotNull(r.getListOfIngredients());
		assertTrue(r.getListOfIngredients().get(0).getIngredientName().equals("cheese"));
	}
	
	@Test
	public void testGetListOfIngredientsAsString(){
		StringBuilder ing = new StringBuilder();
		RecipeData rec = new RecipeData();
		
		rec.addIngredient("water", "1", "c");
		rec.addIngredient("beef", "2", "oz");
		rec.addIngredient("stock", "500", "mL");
		
		ing.append("1 c water\n");
		ing.append("2 oz beef\n");
		ing.append("500 mL stock");
		
		assertTrue(ing.toString().equals(rec.getListOfIngredientsAsString()));
	}
	
	@Test
	public void testSetListOfIngredients(){
		StringBuilder ing = new StringBuilder();
		RecipeData rec = new RecipeData();
		ArrayList<Ingredient> list = new ArrayList<Ingredient>();
		list.add(new Ingredient("water", "1", "c"));
		list.add(new Ingredient("beef", "2", "oz"));
		list.add(new Ingredient("stock", "500", "mL"));
		
		ing.append("1 c water\n");
		ing.append("2 oz beef\n");
		ing.append("500 mL stock");
		
		rec.setListOfIngredients(ing.toString());
		
		assertTrue(compareListOfIngredients(rec.getListOfIngredients(), list));
	}
	
	private boolean compareListOfIngredients(
			ArrayList<Ingredient> first, ArrayList<Ingredient> second){
		boolean isSame = true;
		Ingredient i1 = null;
		Ingredient i2 = null;
		
		if(first.size() != second.size()){
			isSame = false;
		}
		else{
			for(int i = 0; i < first.size(); i++){
				i1 = first.get(i);
				i2 = second.get(i);
				if(i1.toString().compareTo(i2.toString()) != 0){
					isSame = false;
				}
			}
		}
		
		return isSame;
	}

	@Test
	public void testGetInstructions() {
		String instr = "Instructions";
		RecipeData rec = new RecipeData();
		rec.setInstructions(instr);
		assertTrue(instr.equals(rec.getInstructions()));
	}

	@Test
	public void testSetInstructions() {
		String instr = "Instructions";
		RecipeData rec = new RecipeData();
		rec.setInstructions(instr);
		assertTrue(rec.getInstructions().equals(instr));
	}

	@Test
	public void testGetTags() {
		String tg = "Tags";
		RecipeData rec = new RecipeData();
		rec.setTags(tg);
		assertTrue(tg.equals(rec.getTags()));
	}

	@Test
	public void testSetTags() {
		String tg = "Tags";
		RecipeData rec = new RecipeData();
		rec.setTags(tg);
		assertTrue(rec.getTags().equals(tg));
	}

	@Test
	public void testConvertUnits() {
		RecipeData r = new RecipeData();
		r.addIngredient("water", "1", Ingredient.UNIT_METRIC_L);
		
		r.convertUnits();
		
		assertEquals("Should be equal", "0.26 gal water", r.getListOfIngredientsAsString());
	}

	@Test
	public void testScaleAmount() {
		RecipeData r = new RecipeData();
		ArrayList<Ingredient> l = null;
		r.addIngredient("Eggs", "2", "");
		r.addIngredient("Milk", "1", "Tbsp");
		
		r.scaleAmount(2);
		
		l = r.getListOfIngredients();
		
		assertEquals("Should be equal", "4.00", l.get(0).getAmount());
		assertEquals("Should be equal", "2.00", l.get(1).getAmount());
	}
	
	@Test
	public void testAddIngredient(){
		RecipeData r = new RecipeData();
		r.addIngredient("milk", "2", Ingredient.UNIT_STANDARD_FL_OZ);
		assertEquals("Should be equal", "2 fl oz milk", r.getListOfIngredients().get(0).toString());
	}

}
